﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using V5_Common;
using System.Data;
using System.Text.RegularExpressions;
using System.Xml;
using System.Text;
using V5_EShop.Data;

namespace V5_EShop.Admin.Tools {
    public partial class Admin_Template : AdminPage {
        protected void Page_Load(object sender, EventArgs e) {
            if (!this.IsPostBack) {
                string action = InputHelper.SetString(Request["Action"]);
                switch (action) {
                    default:
                        Bind_DataList();
                        break;
                }
            }
        }
        /// <summary>
        /// 加载模板数据
        /// </summary>
        private void Bind_DataList() {
            this.MultiViewList.ActiveViewIndex = 0;
            this.rpList.DataSource = V5.Template.GetAllTemplateInfo(Server.MapPath("~/Template/"));
            this.rpList.DataBind();
        }
        /// <summary>
        /// 设置默认模板
        /// </summary>
        protected void btnSetDefaultTemplate_Click(object sender, EventArgs e) {
            //设置默认模板 同时把Url配置文件更新
            string SelectTemplate = InputHelper.SetString(Request["SelectTemplate"]);
            if (SelectTemplate.Trim() != "") {
                Bind_ReLoadTemplate(SelectTemplate);
                ModelConfig mConfig = new ModelConfig(p => p.ID == 1);
                mConfig.TemplatePath = SelectTemplate;
                mConfig.Update();
                V5.Config.Clear();
                V5.Config.Init();
                DataCacheHelper.RemoveAllCache();
            }
            this.Bind_DataList();
        }
        /// <summary>
        /// 设置默认模板
        /// </summary>
        /// <param name="templatePath"></param>
        /// <returns></returns>
        protected string Bind_TemplatePath(string templatePath) {
            string flag = string.Empty;
            if (V5.Config.View["TemplatePath"].ToString().ToLower() ==
                templatePath.ToLower()) {
                flag = "checked=\"checked\" ";
            }
            return flag;
        }

        protected void rpList_ItemCommand(object source, RepeaterCommandEventArgs e) {
            string eName = InputHelper.SetString(e.CommandName);
            string templatePath = InputHelper.SetString(e.CommandArgument);
            switch (eName.ToLower()) {
                case "editurl":
                    Response.Redirect("Sys_TemplateUrl.aspx?Path=" + templatePath);
                    break;
                case "del":
                    Bind_DeleteTemplate(templatePath);
                    break;
            }
        }
        /// <summary>
        /// 重新载入模板
        /// </summary>
        /// <param name="templatePath"></param>
        private void Bind_ReLoadTemplate(string templatePath) {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<rewriter>");
            sb.AppendLine(@"<rewrite url=""^(/.+(\.gif|\.png|\.jpg|\.ico|\.css|\.js|\.xml|\.axd)(\?.+)?)$"" 	to=""$1"" processing=""stop"" />");
            sb.AppendLine(@"<unless url=""/(.+)/(\d+)-(\d+).html"">");
            sb.AppendLine(@"<unless url=""/admin/(.+)"">");
            sb.AppendLine(@"<unless url=""/Includes/(.+)"">");
            sb.AppendLine(@"<unless url=""/template/(.+)"">");

            string rewriteUrl = string.Empty;
            string oldUrl = string.Empty;
            List<RewriteUrl> ListRewriteUrl = LoadSettingsUrls(templatePath);
            foreach (RewriteUrl url in ListRewriteUrl) {
                sb.AppendLine(@"<rewrite url=""^" + url.Url + @""" to=""" + url.To.Replace("&", "&amp;") + @""" processing=""stop"" />");
            }

            sb.AppendLine(@"<rewrite url=""^/(.+)/(.+)-(.+)-(\d+)-(\d+).aspx$"" to=""/index.aspx?t=$1/$2&amp;S=$3&amp;ClassID=$4&amp;ID=$4&amp;PageID=$5"" processing=""stop"" />");
            sb.AppendLine(@"<rewrite url=""^/(.+)/(.+)-(.+)-(\d+)-(\d+).htm$"" to=""/index.aspx?t=$1/$2&amp;S=$3&amp;ClassID=$4&amp;ID=$4&amp;PageID=$5"" processing=""stop"" />");

            sb.AppendLine(@"<rewrite url=""^/(.+)/(.+)-(\d+)-(\d+).aspx$"" to=""/index.aspx?t=$1/$2&amp;ClassID=$3&amp;ID=$3&amp;PageID=$4"" processing=""stop"" />");
            sb.AppendLine(@"<rewrite url=""^/(.+)/(.+)-(\d+)-(\d+).htm$"" to=""/index.aspx?t=$1/$2&amp;ClassID=$3&amp;ID=$3&amp;PageID=$4"" processing=""stop"" />");

            sb.AppendLine(@"<rewrite url=""^/(.+)-(.+)-(\d+)-(\d+).aspx$"" to=""/index.aspx?t=$1&amp;S=$2&amp;ClassID=$3&amp;ID=$3&amp;PageID=$4"" processing=""stop"" />");
            sb.AppendLine(@"<rewrite url=""^/(.+)-(.+)-(\d+)-(\d+).htm$"" to=""/index.aspx?t=$1&amp;S=$2&amp;ClassID=$3&amp;ID=$3&amp;PageID=$4"" processing=""stop"" />");

            sb.AppendLine(@"<rewrite url=""^/(.+)-(\d+)-(\d+).aspx$"" to=""/index.aspx?t=$1&amp;ClassID=$2&amp;ID=$2&amp;PageID=$3"" processing=""stop"" />");
            sb.AppendLine(@"<rewrite url=""^/(.+)-(\d+)-(\d+).htm$"" to=""/index.aspx?t=$1&amp;ClassID=$2&amp;ID=$2&amp;PageID=$3"" processing=""stop"" />");

            sb.AppendLine(@"<rewrite url=""^/(.+)-(\d+).aspx$"" to=""/index.aspx?t=$1&amp;ID=$2"" processing=""stop"" />");
            sb.AppendLine(@"<rewrite url=""^/(.+)-(\d+).htm$"" to=""/index.aspx?t=$1&amp;ID=$2"" processing=""stop"" />");

            sb.AppendLine(@"<rewrite url=""^/(.+).aspx$"" to=""/index.aspx?t=$1"" processing=""stop"" />");
            sb.AppendLine(@"<rewrite url=""^/(.+).htm$"" to=""/index.aspx?t=$1"" processing=""stop"" />");
            sb.AppendLine(@"</unless>");
            sb.AppendLine(@"</unless>");
            sb.AppendLine(@"</unless>");
            sb.AppendLine(@"</unless>");

            sb.AppendLine("</rewriter>");
            FsoHelper fso = new FsoHelper();
            fso.CreateFile("config/urls.config", sb.ToString());
        }
        /// <summary>
        /// 删除模板
        /// </summary>
        /// <param name="templatePath"></param>
        private void Bind_DeleteTemplate(string templatePath) {
            string SelectTemplate = InputHelper.SetString(Request["SelectTemplate"]);
            if (SelectTemplate == templatePath) {
                Response.Write("该模板正在使用中,不可删除!");
            }
            else {
                FsoHelper fso = new FsoHelper();
                fso.DeleteDir("template\\" + templatePath + "\\", true);
                Response.Redirect("Admin_Template.aspx", true);
            }
        }


        public List<RewriteUrl> LoadSettingsUrls(string templatePath) {
            List<RewriteUrl> ListRewriteUrl = new List<RewriteUrl>();
            try {
                string path = HttpContext.Current.Server.MapPath("~/Template/") + templatePath + "/settings.config";
                XmlDocument xml = new XmlDocument();
                xml.Load(path);
                //获取所有Url链接
                XmlNodeList urlNodes = xml.SelectNodes("templatesettings/urls/url");
                foreach (XmlNode urlNode in urlNodes) {
                    RewriteUrl rurl = new RewriteUrl();
                    string url = urlNode.Attributes["url"].Value.ToString();
                    string to = urlNode.Attributes["to"].Value.ToString();
                    rurl.Url = url;
                    rurl.To = to;
                    ListRewriteUrl.Add(rurl);
                }
            }
            catch (Exception) { }
            return ListRewriteUrl;
        }

        public class RewriteUrl {
            public string Url { get; set; }
            public string To { get; set; }
        }
    }
}